package com.file;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 输入流与输出流
 */
public class 字节流 {
    /**
     * file输入流
     */
    public void fileInput(){
       String path = "D:\\7788\\fuck\\random.txt";
       File f  = new File(path);
       FileInputStream in = null;
        try {
            in = new FileInputStream(f);
            byte[] b = new byte[1024];
            int i ;
            while ((i=in.read(b)) != -1){
                String s = new String(b,0,i);
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 输入流与输出流
     */
    public void inputAndOut(){
        byte[] b = new byte[]{-1,2,4,5,6,7};
        ByteArrayInputStream bInput = new ByteArrayInputStream(b,0,10);
        int i = bInput.read();
        while (i != -1) {
            System.out.println(String.format("原值:%s,转换值:%s", (byte) i, i));
            i = bInput.read();

        }
        //输出流

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        bout.write(b,0,6);
        System.out.println(String.format("输出流字节:%s",bout.size()));
        byte[] b2 = bout.toByteArray();
        System.out.println(Arrays.toString(b2));
    }

    /**
     * 文件输出流
     */
    public void fileOut(){

        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream("D:\\7788\\fuck\\random.txt");
            out = new FileOutputStream("D:\\7788\\fuck\\sister.txt");
            int i;
            byte[] b = new byte[1024];
            while ((i=in.read(b)) != -1){
                out.write(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
    public void fileOut2(){
        FileOutputStream out = null;
        Scanner scanner = new Scanner(System.in );
        try {
            out = new FileOutputStream("d:\\7788\\fuck\\one.txt");
            String s = scanner.next();
            byte[] b = s.getBytes();
            out.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        字节流 inputOut = new 字节流();
       // inputOut.fileInput();
       // inputOut.fileOut();
        inputOut.fileOut2();
    }
}
